<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of soaUsers
 *
 * @author dxx
 */

namespace app\common\model\activity;

class SalesChannel extends \think\Model {

    protected $table = 'sales_channel';
    protected $connection = ['database' => 'activity'];

    /**
     * 获取一条记录通过条件
     * @param type $condition 条件
     */
    public function getOne($condition=[]){
        return $this->where($condition)->find();
    }
    
    /**
     * 获取渠道列表
     * @param type $params
     * @return type
     */
    public function getList($params) {
 
        if (empty($params['company_id'])) {
            return false;
        }
        $list = $where = [];
        $page = isset($params['page']) ? (int) $params['page'] : 1;
        if (!empty($params['keywords'])) {
            $where['channel_name'] = ['like',"%{$params['keywords']}%"];
        }
        $where['company_id'] = $params['company_id'];
        $count = $this->where($where)->count();

        if (!empty($count)) {
            $pageSize = isset($params['pageSize']) ? (int) $params['pageSize'] : 10;
            $order = !empty($params['order']) ? $params['order'] : 'add_date DESC';
            
            $list = $this->where($where)->page($page,$pageSize)->order($order)->select();
        }

        return [
            'page'  => $page,
            'count' => (int) $count,
            'list'  => $list
        ];
    }
    
    
     /**
     * 获取渠道详情
     * @param type $channelId
     */
    public function getChannelInfo($channelId) {
        
        return $this->field("id,channel_type,channel_name, "
                . "channel_describe,add_date,update_date,company_id,status")->where(['id'=>$channelId])->find();
    }
    
    
    /**
     * 关闭渠道
     * @param type $channelId
     */
    public function switchChannel($channelId, $status) {
        return $this->where(['id' => $channelId])->update(['status' => $status]);
    }

    
    /**
     * 新增渠道
     * @param type $data
     */
    public function addChannel($data) {
        return $this->insertGetId($data);
    }
    
}